package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.myExceptions.AssociatedException;
import com.itheima.pojo.PhysicalExam;
import com.itheima.service.PhysicalExamService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 体检结果管理
 */
@RestController
@RequestMapping("/physicalexam")
public class PhysicalExamController {

    @Reference
    private PhysicalExamService physicalExamService;

    /**
     * 生成体检结果
     *
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody PhysicalExam physicalExam) {
        try {
            //调用服务层
            physicalExamService.add(physicalExam);
        } catch (Exception e) {
            if (e instanceof AssociatedException){
                return new Result(false,"该结果已生成，无法重新生成");
            }
            e.printStackTrace();
            //生成失败
            return new Result(false, MessageConstant.ADD_PHYSICALEXAM_FAIL);
        }


        //生成成功
        return new Result(true, MessageConstant.ADD_PHYSICALEXAM_SUCCESS);

    }
}
